package com.code2roc.fastface.bizlogic;

import com.code2roc.fastface.eums.FaceEnum;
import com.code2roc.fastface.model.FaceRegistDO;
import com.code2roc.fastface.model.RegistResult;
import com.code2roc.fastface.service.IFaceRegistService;
import com.code2roc.fastface.util.CommonUtil;
import com.code2roc.fastface.util.ConvertOp;
import com.code2roc.fastface.util.DateUtil;
import com.code2roc.fastface.util.SeetaFaceHepler;
import com.seeta.sdk.util.SeetafaceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Component
public class FaceRegistLogic {
    @Autowired
    private SeetaFaceHepler seetaFaceHepler;
    @Autowired
    private IFaceRegistService faceRegistService;

    public RegistResult addFace(String userID, String userName, String base64Image) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        long registIndex = -999;
        try {
            BufferedImage image = ConvertOp.convertBase64ToImage(base64Image.split("base64,")[1]);
            FaceEnum.CheckImageFaceStatus checkImageFaceStatus = seetaFaceHepler.getFace(image);
            if (checkImageFaceStatus.get_value() == FaceEnum.CheckImageFaceStatus.NoFace.get_value()) {
                status = -1;
                message = "未检测到人脸";
            } else if (checkImageFaceStatus.get_value() == FaceEnum.CheckImageFaceStatus.MoreFace.get_value()) {
                status = -1;
                message = "检测到多张人脸";
            } else {
                registIndex = seetaFaceHepler.registFace(image);
            }
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }

        if (status == 0) {
            FaceRegistDO faceRegistDO = new FaceRegistDO();
            faceRegistDO.setUserID(userID);
            faceRegistDO.setUserName(userName);
            faceRegistDO.setRegistDate(DateUtil.getDateFormatToSecond());
            faceRegistDO.setUpdateDate(DateUtil.getDateFormatToSecond());
            faceRegistDO.setBase64Image(base64Image);
            faceRegistDO.setRegistIndex(registIndex + "");
            faceRegistService.insert(faceRegistDO);
        }
        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }

    public RegistResult addFaceV2(String userID, String userName, String filePath) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        long registIndex = -999;
        try {
            BufferedImage image = SeetafaceUtil.toBufferedImage(filePath);
            FaceEnum.CheckImageFaceStatus checkImageFaceStatus = seetaFaceHepler.getFace(image);
            if (checkImageFaceStatus.get_value() == FaceEnum.CheckImageFaceStatus.NoFace.get_value()) {
                status = -1;
                message = "未检测到人脸";
            }else if (checkImageFaceStatus.get_value() == FaceEnum.CheckImageFaceStatus.MoreFace.get_value()) {
                status = -1;
                message = "检测到多张人脸";
            }  else {
                registIndex = seetaFaceHepler.registFace(image);
            }
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }

        if (status == 0) {
            FaceRegistDO faceRegistDO = new FaceRegistDO();
            faceRegistDO.setUserID(userID);
            faceRegistDO.setUserName(userName);
            faceRegistDO.setRegistDate(DateUtil.getDateFormatToSecond());
            faceRegistDO.setUpdateDate(DateUtil.getDateFormatToSecond());
            faceRegistDO.setBase64Image(ConvertOp.convertImageToBase64(filePath));
            faceRegistDO.setRegistIndex(registIndex + "");
            faceRegistService.insert(faceRegistDO);
        }
        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }

    public RegistResult updateFace(String userID, String base64Image) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        long registIndex = -999;
        FaceRegistDO faceRegistDO = faceRegistService.selectOne(userID);
        try {
            BufferedImage image = ConvertOp.convertBase64ToImage(base64Image.split("base64,")[1]);
            FaceEnum.CheckImageFaceStatus checkImageFaceStatus = seetaFaceHepler.getFace(image);
            if (checkImageFaceStatus.get_value() == FaceEnum.CheckImageFaceStatus.NoFace.get_value()) {
                status = -1;
                message = "未检测到人脸";
            }else if (checkImageFaceStatus.get_value() == FaceEnum.CheckImageFaceStatus.MoreFace.get_value()) {
                status = -1;
                message = "检测到多张人脸";
            }  else {
                seetaFaceHepler.deleteFace(Long.parseLong(faceRegistDO.getRegistIndex()));
                registIndex = seetaFaceHepler.registFace(image);
            }
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }

        if (status == 0) {
            faceRegistDO.setUpdateDate(DateUtil.getDateFormatToSecond());
            faceRegistDO.setBase64Image(base64Image);
            faceRegistDO.setRegistIndex(registIndex + "");
            faceRegistService.update(faceRegistDO);
        }
        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }

    public RegistResult deleteFace(String userID) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        FaceRegistDO faceRegistDO = faceRegistService.selectOne(userID);
        try {
            seetaFaceHepler.deleteFace(Long.parseLong(faceRegistDO.getRegistIndex()));
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }
        if (status == 0) {
            faceRegistService.delete(userID);
        }
        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }

    public RegistResult queryFace(String base64Image) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        try {
            BufferedImage image = ConvertOp.convertBase64ToImage(base64Image.split("base64,")[1]);
            long queryIndex = seetaFaceHepler.queryFace(image);
            if (queryIndex >= 0) {
                message = queryIndex + "";
            } else {
                status = -1;
                message = "未匹配到人脸";
            }
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }

        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }

    public void resetFaceDBIndex() throws Exception {
        seetaFaceHepler.resetFaceDataBase();
        List<FaceRegistDO> faceRegistDOList = faceRegistService.selectAllList();
        for (FaceRegistDO faceRegistDO : faceRegistDOList) {
            BufferedImage image = ConvertOp.convertBase64ToImage(faceRegistDO.getBase64Image().split("base64,")[1]);
            long registIndex = seetaFaceHepler.registFace(image);
            faceRegistDO.setRegistIndex(registIndex + "");
            faceRegistService.update(faceRegistDO);
        }
    }

    public RegistResult checkFace(String base64Image) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        try {
            BufferedImage image = ConvertOp.convertBase64ToImage(base64Image.split("base64,")[1]);
            FaceEnum.CheckImageFaceStatus checkImageFaceStatus = seetaFaceHepler.getFace(image);
            message = checkImageFaceStatus.get_name();
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }

        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }

    public RegistResult compareFace(String sourceBase64Image,String compareBase64Image) {
        RegistResult registResult = new RegistResult();
        int status = 0;
        String message = "success";
        try {
            BufferedImage source = ConvertOp.convertBase64ToImage(sourceBase64Image.split("base64,")[1]);
            BufferedImage compare = ConvertOp.convertBase64ToImage(compareBase64Image.split("base64,")[1]);
            FaceEnum.CompareImageFaceStatus compareImageFaceStatus = seetaFaceHepler.compareFace(source,compare);
            message = compareImageFaceStatus.get_name();
        } catch (Exception e) {
            status = -1;
            message = "exception";
            e.printStackTrace();
        }

        registResult.setStatus(status);
        registResult.setMessage(message);
        return registResult;
    }


}
